package com.lixinxinlove.jaychoumusic.page

import android.annotation.SuppressLint
import android.content.ComponentName
import android.content.Intent
import android.content.ServiceConnection
import android.os.IBinder
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.animation.core.*
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Colors
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Slider
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.rotate
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Color.Companion.White
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavController
import com.lixinxinlove.jaychoumusic.R
import com.lixinxinlove.jaychoumusic._interface.IMusicPlay
import com.lixinxinlove.jaychoumusic.service.MusicPlayService

/**
 *   author ： lixinxin
 *   time    ： 2021-12-24
 *   email：895330766@qq.com
 */

@Composable
fun MusicPlayScreen() {

    val context = LocalContext.current
    val mContext = LocalContext

    lateinit var iMusicPlay: IMusicPlay

    val serviceConnection = object : ServiceConnection {

        @SuppressLint("SetTextI18n")
        override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
            iMusicPlay = service as MusicPlayService.MyBinder
            iMusicPlay.play()
        }

        override fun onServiceDisconnected(name: ComponentName?) {
            //取消绑定服务
        }
    }


    fun bindService() {
        val bindIntent = Intent(context, MusicPlayService::class.java)
        context.startService(bindIntent)
        context.bindService(bindIntent, serviceConnection, AppCompatActivity.BIND_ABOVE_CLIENT)
    }


    fun unBindService() {
        context.unbindService(serviceConnection)
        val bindIntent = Intent(context, MusicPlayService::class.java)
        context.stopService(bindIntent)
    }


    var state by remember {
        mutableStateOf(true)
    }

    var sliderValue by remember {
        mutableStateOf(0.5f)
    }
    val degreesValue by animateFloatAsState(
        targetValue = if (state) 0f else 360f,

        animationSpec = infiniteRepeatable(
            animation = tween(
                durationMillis = 18000,
                easing = LinearEasing
            ),
            repeatMode = RepeatMode.Restart,


            )
    )


    LaunchedEffect(key1 = state, block = {
        state = false
    })


    Column(

        modifier = Modifier
            .fillMaxSize()
            .background(color = White),
        horizontalAlignment = Alignment.CenterHorizontally
    ) {

        Row(
            modifier = Modifier
                .fillMaxWidth()
                .height(48.dp),
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.Center
        ) {
            Text(
                text = "title", style =
                TextStyle(
                    color = MaterialTheme.colors.primary,
                    fontSize = 18.sp,
                    fontWeight = FontWeight.Bold
                )
            )
        }


        Spacer(modifier = Modifier.padding(top = 20.dp))

        Image(
            painter = painterResource(id = R.mipmap.ic_logo),
            contentDescription = "图片",
            modifier = Modifier
                .size(160.dp)
                .clip(RoundedCornerShape(80.dp))
                .rotate(degrees = degreesValue)
        )


        Spacer(modifier = Modifier.padding(top = 20.dp))




        Slider(value = sliderValue,
            modifier = Modifier
                .fillMaxWidth()
                .height(20.dp)
                .padding(vertical = 16.dp, horizontal = 16.dp),
            onValueChange = {

            })


        Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.Center) {

            Image(
                painter = painterResource(id = R.mipmap.ic_left),
                contentDescription = "上一曲",
                modifier = Modifier
                    .size(24.dp)
                    .weight(1f)
            )
            Image(
                painter = painterResource(id = R.mipmap.ic_play),
                contentDescription = "播放",
                modifier = Modifier
                    .size(24.dp)
                    .weight(1f)
                    .clickable(onClick = ::bindService)
            )
            Image(
                painter = painterResource(id = R.mipmap.ic_right),
                contentDescription = "播放",
                modifier = Modifier
                    .size(24.dp)
                    .weight(1f)
            )

        }

    }


}


@Preview
@Composable
fun MusicPlayScreenPreview() {
    MusicPlayScreen()
}

